//
//  MeatShopCloseView.m
//  PigMarket
//
//  Created by 张敬 on 2019/8/26.
//  Copyright © 2019年 pigmarket.oc. All rights reserved.
//

#import "MeatShopCloseView.h"

@interface MeatShopCloseView() <CAAnimationDelegate>

@property (nonatomic, assign) NSInteger count;


@end


@implementation MeatShopCloseView

#pragma mark - lifecycle methods

- (void)setupUI{
    
    self.backgroundColor = mBackgroudColor;
    [self addSubview:self.messageLogo];
    [self addSubview:self.boardLab];
    [self addSubview:self.messageBtn];
    
    [self startLeftAnnimation];
}

- (void)setupLayout
{
    self.height = 302;
    self.width = 188;
    self.mj_x = (KScreenWidth - self.width)/2.0;
    self.mj_y = 0;
    
    {
        _messageLogo.mj_y = 0;
        _messageLogo.mj_x = 0;
        _messageLogo.width = self.width;
        _messageLogo.height = 161;
    }
    
    {
        _messageLab.mj_y = 81;
        _messageLab.mj_x = 0;
        _messageLab.width = self.width;
        _messageLab.height = 80;
    }
    
    {
        _boardLab.mj_y = _messageLogo.bottom + 20;
        _boardLab.mj_x = 0;
        _boardLab.width = self.width;
        _boardLab.height = 36;
    }
    
    {
        _messageBtn.mj_y = _boardLab.bottom + 43;
        _messageBtn.mj_x = (self.width - 126)/2.0;
        _messageBtn.width = 126;
        _messageBtn.height = 36;
    }
}

#pragma mark private methods

- (void)startLeftAnnimation{
    
    /// 左边👈
    //初始化一个动画
    CABasicAnimation *baseAnimation = [CABasicAnimation animation];
    //动画运动的方式，现在指定的是围绕Z轴旋转
    baseAnimation.keyPath = @"transform.rotation.z";
    //动画持续时间
    baseAnimation.duration = 0.5;
    //开始的角度
    baseAnimation.fromValue = [NSNumber numberWithFloat:0];
    //结束的角度
    baseAnimation.toValue = [NSNumber numberWithFloat:M_PI/8];
    //动画的运动方式
    baseAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    //是否反向移动动画
    baseAnimation.autoreverses = YES;
    //动画的代理
    baseAnimation.delegate = self;
    //动画结束后的状态
    baseAnimation.fillMode = kCAFillModeForwards;
    
    [baseAnimation setValue:@"left" forKey:@"left"];
    
    [self.messageLogo.layer addAnimation:baseAnimation forKey:@"left"];
    
}


- (void)startRightAnnimation{
    /// 右边👉
    //初始化一个动画
    CABasicAnimation *baseAnimation2 = [CABasicAnimation animation];
    //动画运动的方式，现在指定的是围绕Z轴旋转
    baseAnimation2.keyPath = @"transform.rotation.z";
    //动画持续时间
    baseAnimation2.duration = 0.5;
    //开始的角度
    baseAnimation2.fromValue = [NSNumber numberWithFloat:0];
    //结束的角度
    baseAnimation2.toValue = [NSNumber numberWithFloat:-M_PI/8];
    //动画的运动方式
    baseAnimation2.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    //是否反向移动动画
    baseAnimation2.autoreverses = YES;
    //动画的代理
    baseAnimation2.delegate = self;
    //动画结束后的状态
    baseAnimation2.fillMode = kCAFillModeForwards;
    
    [baseAnimation2 setValue:@"right" forKey:@"right"];
    
    [self.messageLogo.layer addAnimation:baseAnimation2 forKey:@"right"];
}


- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
    
    if (flag) {
        
        if ([[anim valueForKey:@"left"] isEqualToString:@"left"]) {
            //检测到左边结束后,开始右边的动画
            [self startRightAnnimation];
        }else if ([[anim valueForKey:@"right"] isEqualToString:@"right"]) {
            
           // [self startLeftAnnimation];
            self.count++;
            if (self.count <= 5) {

                //检测到右边动画的时候,开始左边的动画
                [self startLeftAnnimation];
            }else {



                /// 移除很重要 要不然不会调用dealloc
                [self.messageLogo.layer removeAllAnimations];

            }
        }
    }
}


#pragma mark - setter methods

#pragma mark - getter methods

- (UIImageView *)messageLogo{
    
    if (!_messageLogo) {
        _messageLogo = [[UIImageView alloc] init];
        _messageLogo.image = [UIImage imageNamed:@"meat_shop_message"];
        _messageLogo.layer.anchorPoint = CGPointMake(0.5, 0);

        [_messageLogo addSubview:self.messageLab];
        
    }
    return _messageLogo;
}

- (UILabel *)messageLab{
    
    if (!_messageLab) {
        _messageLab = [UILabel makeLabelWithText:@"小主外出，有臀尖肉，\n想要请留言～" textColor:UIColorFromRGB(0x333333) font:mMediumFont(15) textAliment:1];
        _messageLab.numberOfLines = 0;
    }
    return _messageLab;
}

- (UILabel *)boardLab{
    
    if (!_boardLab) {
        _boardLab = [UILabel makeLabelWithText:@"店铺打烊 \n赶紧给店家留言吧～" textColor:UIColorFromRGB(0xB0B0B0) font:mFont(13) textAliment:1];
        _boardLab.numberOfLines = 0;
    }
    return _boardLab;
}

- (UIButton *)messageBtn{
    
    if (!_messageBtn) {
        _messageBtn = [UIButton makeButtonWithText:@"给店家留言" textColor:KWhiteColor font:mFont(14) textAliment:1 cornerRadius:18];
        [_messageBtn setBackgroundImage:[UIImage imageNamed:@"meat_cart_pay_right"] forState:UIControlStateNormal];
        
        @weakify(self);
        _messageBtn.tapBlock = ^{
            @strongify(self);
            if (self.dealLeaveMessageBlock) {
                self.dealLeaveMessageBlock();
            }
        };
    }
    return _messageBtn;
}


@end
